package com.example.practice3;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.renderscript.ScriptGroup;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private TextView Output1,Output2;
private EditText Input1,Input2;
private Button InputButton,OutPutButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Output1=findViewById(R.id.outPut1);
        Output2=findViewById(R.id.OutPut2);
        Input1=findViewById(R.id.Input1);
        Input2=findViewById(R.id.Input2);
        InputButton=findViewById(R.id.InputButton);
        OutPutButton=findViewById(R.id.OutPutButton);
        InputButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor editor = getSharedPreferences("MyMsg",MODE_PRIVATE).edit();
                if(!Input1.getText().toString().equals("")&&!Input2.getText().toString().equals("")){
                editor.putString("name",Input1.getText().toString());
                editor.putString("ID",Input2.getText().toString());
                editor.apply();
                }
                else
                    Toast.makeText(MainActivity.this,"输入不能为空",Toast.LENGTH_SHORT).show();

            }
        });
        OutPutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences pref = getSharedPreferences("MyMsg",MODE_PRIVATE);
                if(pref.getString("name","NoName").equals("NoName"))
                    Toast.makeText(MainActivity.this,"读取失败！",Toast.LENGTH_SHORT).show();
                else
            {
                Output1.setText(pref.getString("name","NoName"));
                Output2.setText(pref.getString("ID","NoName"));
            }
            }
        });
    }

}